package com.xy.etl.service;

import cn.hutool.core.bean.BeanUtil;
import com.xy.core.constant.FnKit;
import com.xy.etl.kit.IEtlServer;
import com.xy.etl.model.JobVO;
import lombok.extern.slf4j.Slf4j;

/**
 * 作业运行工具
 */
@Slf4j
public abstract class JobServer implements IEtlServer {

    /**
     * 作业信息
     *
     * @return
     */
    public abstract JobVO getJobInfo();


    /**
     * 作业
     */
    public synchronized final void process() {
        //取得分片信息
        JobVO stdVO = getJobInfo();
        if (stdVO == null) {
            throw new RuntimeException("作业信息为空");
        }
        if (FnKit.isEmpty(stdVO.getJobName())) {
            throw new RuntimeException("作业名称为空");
        }
        if (stdVO.getInterval() == null || stdVO.getInterval() <= 0) {
            throw new RuntimeException("时间间隔必须是正整数");
        }
        if (stdVO.getTimeout() == null || stdVO.getTimeout() <= 0) {
            throw new RuntimeException("超时时间必须是正整数");
        }
        JobVO nowVO = new JobVO();
        BeanUtil.copyProperties(stdVO, nowVO);
        nowVO.setJobRecord(null);
        log.info(Thread.currentThread().getId() + " 作业开始 ");
        String jobRecord = schedule(nowVO);
        if (jobRecord != null) {
            nowVO.setJobRecord(jobRecord);
        }
        log.info(Thread.currentThread().getId() + " 作业结束");
    }

    /**
     * 作业
     */
    public abstract String schedule(JobVO jobVO);


}
